﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using Membership.Library;
using Membership.Library.Web; 
using System.Windows.Browser;
using System.Windows.Data;
using System.Windows.Navigation;

namespace Membership.UI.PersonnelManagement
{
    public partial class EmployeeList : Page
    {

        MembershipDomainContext _context = new MembershipDomainContext();

        public EmployeeList()
        {
            InitializeComponent();

        }

        public void CustomInitializeComponent()
        {
        
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void AddStackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PersonnelManagement/EmployeeEdit.xaml", UriKind.Relative));
        }

        private void QueryStackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            RefreshDataGrid();
        }

        private void RefreshDataGrid()
        {
            //TestClass c = new TestClass();
            //ContextDataGrid.ItemsSource = c.GetEmployeeList();

            LodingBusyIndicator.IsBusy = true;
            PagedCollectionView itemListView = new PagedCollectionView(_context.Employees);
            ContextDataGrid.ItemsSource = itemListView;
            ContextDataPager.Source = itemListView;
            _context.Load(_context.GetEmployeeQuery());
            LodingBusyIndicator.IsBusy = false;
        }

        void OnDelete(object sender, MouseButtonEventArgs e)
        {

            if (HtmlPage.Window.Confirm("确定删除？"))
            {
                Employee record1 = ContextDataGrid.SelectedItem as Employee;
                _context.Employees.Remove(record1);
                _context.SubmitChanges();
            }

            RefreshDataGrid();
        }

        void OnUpdate(object sender, MouseButtonEventArgs e)
        {

            //((System.Windows.Controls.Frame)this.Parent).Content =new EmployeeEdit() { DataContext = ContextDataGrid.SelectedItem as Employee };
            
            Employee record1 = ContextDataGrid.SelectedItem as Employee;
            NavigationService.Navigate(new Uri("/PersonnelManagement/EmployeeEdit.xaml?EmployeeId=" + record1.EmployeeId, UriKind.Relative));
        
        }


        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

    }
}
